﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ClientList : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_Client(0);
            }
        }

        protected void DataBind_Client(int recordcount)
        {
            if (recordcount == 0)
            {
                this.AspNetPager_Content.CurrentPageIndex = 1;
            }
            int merchantId = 1;
            if (Session["MerchantId"] != null)
            {
                merchantId = int.Parse(Session["MerchantId"].ToString());
            }
            int count = recordcount;
            int pageIndex = this.AspNetPager_Content.CurrentPageIndex;
            int pageSize = this.AspNetPager_Content.PageSize;
            this.rpClientList.DataSource = ServiceLocator.IClientService.GetNxClientCollection(merchantId, pageIndex, pageSize, ref count);
            this.rpClientList.DataBind();
            this.AspNetPager_Content.RecordCount = count;
            if (count == 0)
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "NO_USER") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','ClientCreate.aspx')", this.Page);
            }
        }

        protected void rpClientList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NxClient client = (NxClient)e.Item.DataItem;

                if (client != null)
                {
                    Label lbNum = (Label)e.Item.FindControl("lbNum");
                    lbNum.Text = (e.Item.ItemIndex + 1).ToString();
                    Label lbCellPhone = (Label)e.Item.FindControl("lbCellPhone");
                    lbCellPhone.Text = client.Cellphone;
                    Label lbName = (Label)e.Item.FindControl("lbName");
                    lbName.Text = client.UserName;
                    Label lbCompany = (Label)e.Item.FindControl("lbCompany");
                    lbCompany.Text = client.Company;
                    Label lbSex = (Label)e.Item.FindControl("lbSex");
                    lbSex.Text = client.Sex.ToString() == "1" ? (String)GetGlobalResourceObject("Default", "MALE") : (String)GetGlobalResourceObject("Default", "FEMALE");
                    Label lbAge = (Label)e.Item.FindControl("lbAge");
                    lbAge.Text = client.Age.ToString() + (String)GetGlobalResourceObject("Default", "Age");
                    HyperLink hlView = (HyperLink)e.Item.FindControl("hlView");
                    hlView.NavigateUrl = string.Format("ClientView.aspx?ClientId={0}", client.ClientId.ToString());
                    HyperLink hlModify = (HyperLink)e.Item.FindControl("hlModify");
                    hlModify.NavigateUrl = string.Format("ModifyClient.aspx?ClientId={0}", client.ClientId.ToString());
                    HyperLink hlDel = (HyperLink)e.Item.FindControl("hlDel");
                    hlDel.NavigateUrl = string.Format("Operate.aspx?ClientId={0}&OpType=ClientDel", client.ClientId.ToString());
                }
            }
        }

        protected void AspNetPager_Content_PageChanged(object sender, EventArgs e)
        {
            DataBind_Client(this.AspNetPager_Content.RecordCount);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            JavaScript.WriteJs("history.go(-2);", this.Page);
        }

    }
}
